package com.lq.ecoldchain.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.lq.ecoldchain.config.AppConfig;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    	api = WXAPIFactory.createWXAPI(this, AppConfig.WX_KEY_APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e("WXPayEntryActivity", "onPayFinish:" + resp.errCode+"/"+resp.errStr+"/"+resp.transaction+"/"+resp.getType());

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			Toast.makeText(this, resp.errCode == -2?"支付失败":"支付成功", Toast.LENGTH_SHORT).show();

			Intent intent = new Intent();
//			intent.setAction(MyBroadcastReceiver.ORDER_PAY_ACTION);
//			intent.putExtra(RESULT, resp.errCode == -2?"支付失败":"支付成功");
			sendBroadcast(intent);
		}

		finish();
	}
}